package cn.edu.cug.cs.gtl.plugins.catalog.tree;

import cn.edu.cug.cs.gtl.apis.ui.datake.catalogtree.CatalogTree;
import cn.edu.cug.cs.gtl.apis.ui.tree.TreeNodeData;
import cn.edu.cug.cs.gtl.protos.DataCatalog;
import cn.edu.cug.cs.gtl.protos.DataGroup;
import cn.edu.cug.cs.gtl.protos.DataItem;
import cn.edu.cug.cs.gtl.utils.postgresql.PostgresqlUtil;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Locale;
import java.util.ResourceBundle;

public class ActionTools {
    public ActionTools() {
    }

    public static void Rename(CatalogTree tree, Object object) throws SQLException {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("catalog-resource", Locale.getDefault());
        DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode) object;
        TreeNodeData treeNodeData = (TreeNodeData) defaultMutableTreeNode.getUserObject();
        Object obj = treeNodeData.getRefObject();
        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();

//        String newAlias = JOptionPane.showInputDialog(null, "请输入新的别名：", "输入对话框", JOptionPane.INFORMATION_MESSAGE);
        String newAlias = JOptionPane.showInputDialog(null,
                resourceBundle.getString("ActionTools.new_alias"), null, JOptionPane.INFORMATION_MESSAGE);

        String updateSql = null;
        PostgresqlUtil util = new PostgresqlUtil();
        Connection connection = util.getConnection();
        PreparedStatement statement = null;
        if (newAlias == null)
            return;
        if (obj instanceof DataCatalog) {
            DataCatalog oldCatalog = (DataCatalog) obj;
            DataCatalog newCatalog = oldCatalog.newBuilderForType().setName(oldCatalog.getName()).setAlias(newAlias)
                    .setDescription(oldCatalog.getDescription()).setParentName(oldCatalog.getParentName()).addAllGroupName(oldCatalog.getGroupNameList()).build();

            defaultMutableTreeNode.setUserObject(new TreeNodeData(newCatalog, newCatalog.getAlias(), true));
            updateSql = "UPDATE data_catalog SET alias = ? WHERE name = ?";
            statement = connection.prepareStatement(updateSql);
            statement.setString(1, newCatalog.getAlias());
            statement.setString(2, oldCatalog.getName());
        } else if (obj instanceof DataGroup) {
            DataGroup oldGroup = (DataGroup) obj;
            DataGroup newGroup = oldGroup.newBuilderForType().setName(oldGroup.getName()).setAlias(newAlias)
                    .setDescription(oldGroup.getDescription()).addAllDataItemName(oldGroup.getDataItemNameList()).build();

            defaultMutableTreeNode.setUserObject(new TreeNodeData(newGroup, newGroup.getAlias(), true));
            updateSql = "UPDATE data_group SET alias = ? WHERE name = ?";
            statement = connection.prepareStatement(updateSql);
            statement.setString(1, newGroup.getAlias());
            statement.setString(2, oldGroup.getName());
        } else if (obj instanceof DataItem) {
            DataItem oldItem = (DataItem) obj;
            DataItem newItem = oldItem.newBuilderForType().setName(oldItem.getName()).setAlias(newAlias)
                    .setDescription(oldItem.getDescription()).setKey(oldItem.getKey()).setFormat(oldItem.getFormat()).build();

            defaultMutableTreeNode.setUserObject(new TreeNodeData(newItem, newItem.getAlias(), true));
            updateSql = "UPDATE data_item SET alias = ? WHERE name = ?";
            statement = connection.prepareStatement(updateSql);
            statement.setString(1, newItem.getAlias());
            statement.setString(2, oldItem.getName());
        }
        int ret = 0;
        if (statement != null) {
            ret = statement.executeUpdate();
        }
        if (ret != 1) {
//            JOptionPane.showMessageDialog(null, "更新失败", "错误", JOptionPane.ERROR_MESSAGE);
            JOptionPane.showMessageDialog(null, resourceBundle.getString("ActionTools.update_failed"), resourceBundle.getString("ActionTools.error"), JOptionPane.ERROR_MESSAGE);
            return;
        }
        model.nodeChanged(defaultMutableTreeNode);
    }
}
